<?php

$INPUT = Antz::registry('input');

if(post('selCountry')!='') $_GET['country'] = post('selCountry');



if(get('city')!='' && get('delete')==1){
    // wanting to delete a city
    $cityid = (int) get('city');
    $cityTitle = $DB->oneValue(cms_db_prefix().'iusers_cities', 'title', "id = '{$cityid}'");
    $DB->delete(cms_db_prefix().'iusers_cities', "id = '{$cityid}'");
    $DB->update(cms_db_prefix().'iusers', array('city'=>0), "city = '{$cityid}'");
    Antz::addSuccess($this->Lang('city_deleted'));
};


if(Antz_IntelliForm::submitted('places')){
    switch(post('action')){
        case 'deleteCity':
            $cityid = (int) post('city');
            if($DB->countRows(cms_db_prefix().'iusers_cities', "id = '{$cityid}'")==0){
                Antz::addError($this->Lang('invalid_city'));
            }else{
                $DB->update(cms_db_prefix().'iusers', array('city'=>0), "city = '{$cityid}'");
                $DB->delete(cms_db_prefix().'iusers_cities', "id = '{$cityid}'");
                Antz::addsuccess($this->Lang('city_removed'));
            };
        break;
        case 'addCity':
            $countryId = preg_replace('/[^a-zA-Z0-9]/', '', get('country'));
            if($DB->countRows(cms_db_prefix().'iusers_countries', "id = '{$countryId}'")==0){
                Antz::addError($this->Lang('invalid_country'));
            }else{
                $title = post('pAddCity');
                if(trim($title)==''){
                    Antz::addError($this->Lang('cannot_be_blank'));
                }else{
                    $DB->insert(cms_db_prefix().'iusers_cities', array('country_id'=>$countryId, 'title'=>$title));

                    Antz::addSuccess($this->Lang('city_added'));
                };
            };
        break;
        case 'renameCountry':
            $countryid =   preg_replace('/[^a-zA-Z0-9]/', '', get('country'));
            $error = false;
            $title = post('renameCountryTitle');
            $flag = post('flag');
            $showFlag = (int) post('show_flag');
            if(trim($title) == ''){
                $error = true;
                Antz::addError($this->Lang('cannot_be_blank'));
            };
            if(file_exists($flag['tmp_name'])){
                // copy the image to the flags directory
                $filename = $DB->oneValue(cms_db_prefix().'iusers_countries', 'filename', "id = '{$countryid}'");
                $flagDir = str_replace(DIRECTORY_SEPARATOR, '/', $this->config['root_path']).'/modules/'.$this->GetName().'/images/flags';
                if($filename != '' && file_exists($flagDir.'/'.$filename)){
                   unlink($flagDir.'/'.$filename);
                };
                $ext = substr($flag['tmp_name'], strlen($flag['tmp_name'])-4);
                $validExts = array('.gif', '.jpg', '.png');
                if(in_array(strtolower($ext), $validExts)){
                    $newFilename = $countryid.strtolower($ext);
                    copy($flag['tmp_name'], $flagDir.'/'.$newFilename);
                    $imgObj = Antz::newObject('Antz_Image');
                    $imgObj->resize($flagDir.'/'.$newFilename, $this->GetConfig('flag_width'), $this->GetConfig('flag_height'));
                    $DB->update(cms_db_prefix().'iusers_countries', array('filename'=>$newFilename), "id = '{$countryid}'");
                };
            };
            if(!$error){
                $DB->update(cms_db_prefix().'iusers_countries', array('title'=>$title, 'show_flag'=>$showFlag), "id = '{$countryid}'");
                Antz::addSuccess($this->Lang('country_updated'));
            };
        break;
    };
}





$countryid = preg_replace('/[^a-zA-Z0-9]/', '', get('country', ''));
$countries = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_countries ORDER BY title ASC");
$cities = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_cities WHERE country_id = '{$countryid}' ORDER BY title ASC");
$SMARTY->assign('countryId', $countryid);

$form = Antz::newObject('Antz_Form', 'places');
$form->setAction($actionUrls['places'].'&amp;country='.$countryid);
$block = $form->add('full');
$fs = $block->add($this->Lang('select_country'));
$sel = $fs->addSelect('selCountry', 'Country', get('country'));
$sel->add($this->Lang('please_select'));
foreach($countries as $k=>$v){
    $sel->add($v['title'], $v['id']);
};
$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->lang('select_country'));

$this->smarty->assign('pSelectCountryForm', $form->render());


$countryTitle = $DB->oneValue(cms_db_prefix().'iusers_countries', 'title', "id = '{$countryid}'");

$SMARTY->assign('pSelectedCountryTitle', $countryTitle);

if($countryid != ''){
    // rename the country
    $form = Antz::newObject('Antz_Form', 'places');
    $form->setAction($actionUrls['places'].'&amp;country='.$countryid);
    $block = $form->add('full');
    $fs = $block->add($this->Lang('update_this_country', $countryTitle));
    $fs->addInput('hidden', 'action', '&nbsp;', 'renameCountry');
    $fs->addInput('text', 'renameCountryTitle', $this->Lang('title'), post('newCountryTitle', $DB->oneValue(cms_db_prefix().'iusers_countries', 'title', "id = '{$countryid}'")));
    $fs->addInput('file', 'flag', $this->Lang('flag_icon'), '');
    $chk = $fs->addCheckboxGroup('show_flag');
    $chk->add('show_flag', $this->Lang('show_flag'), '1', (bool) $DB->oneValue(cms_db_prefix().'iusers_countries', 'show_flag', "id = '{$countryid}'"));
    $fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('update'));
    $SMARTY->assign('pRenameCountryForm', $form->render());

    // add a city to the country
    $form = Antz::newObject('Antz_Form', 'places');
    $form->setAction($actionUrls['places'].'&amp;country='.$countryid);
    $block = $form->add('full');
    $fs = $block->add($this->Lang('add_city_to', $countryTitle));
    $fs->addInput('hidden', 'action', '&nbsp;', 'addCity');
    $fs->addInput('text', 'pAddCity', $this->Lang('title'), post('pAddCity'));
    $fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('add_city_to', $countryTitle));
    $SMARTY->assign('pAddCityForm', $form->render());

};



if($countryid != ''){
    $cities = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}iusers_cities WHERE country_id = '{$countryid}' ORDER BY title ASC");
    $SMARTY->assign('pCities', $cities);
    /*$forms = array();
    foreach($cities as $k=>$v){
        $form = new Antz_Form;
        $form->setAction($actionUrls['places'].'&amp;country='.$countryid);
        $block = $form->add('full');
        $fs = $block->add('Rename '.$v['title']);
        $fs->addInput('hidden', 'action', '&nbsp;', 'pRenameCity');
        $fs->addInput('hidden', 'city', '&nbsp;', $v['id']);
        $fs->addInput('text', 'title', 'New name', $v['title']);
        $fs->addInput('submit', 'submitBtn', '&nbsp;', 'Rename');
        $forms[$v['id']] = $form->render();
    };
    $SMARTY->assign('pRenameCityForms', $forms);
    */
}else{
    $SMARTY->assign('pCities', '');
};


echo $this->ProcessTemplate('admin.places.php');

